package edu.towson.cosc.cosc455.ddange1.markdowncompiler;
import java.util.*;
import java.io.*;

public class compiler {
	static String token;
	//static boolean isTag;
    public static Stack<String> info = new Stack<String>();
    public static String fileText = "";
    
    public static mySyntaxAnalyzer syntax = new mySyntaxAnalyzer(); 
    public static mySemanticAnalyzer semantic = new mySemanticAnalyzer();
    
    public static Scanner reader;
    public static BufferedWriter writer;
    
    public static boolean createDocument = true; 
    
    public static void main(String[] args) throws IOException, CompilerException 
    {
    	// 1. check to make sure you have the correct arguments
    	// 2. check to make sure the file has an .mkd extension --> endsWith(".mkd");
    	// 3. initializations
    	
    	//read in the text file
    	boolean fileFound = false;
    	Scanner scan = new Scanner(System.in);
    	String fileName = args[0];
    	if(!fileName.substring(fileName.length()-4).equals(".mkd")){
        	System.exit(0);
    	}
    	else{
    	//TODO check that the file has .mkd    	
    	try{
            reader = new Scanner(new File(fileName));
            String line = "";
            while(reader.hasNextLine())
            {
            	line = reader.nextLine();
            	fileText = fileText + line;
            }
            fileFound = true;
            reader.close();
    	}
    	catch(IOException e)
    	{
    		fileFound = false;
    		System.out.println("File was not found.");
    		reader.close();
    		e.printStackTrace();
    	} 
    	//if the file is found, proceed with compiler functions
    	if(fileFound && createDocument){
    		token="";
        	// 4. call the lexical analyzer to get the first token set
    			//in the syntax analyzer
            // 5. call the start state of the parser, i.e., document()
    		try{
    			syntax.document();
    		}
    		catch(CompilerException e){
    			e.printStackTrace();
    		}
            // ... lexer and parser will then do its thing
            // 6. call the semantic analyzer
    		// 7. write the output file to html
    		writer= new BufferedWriter(new FileWriter(fileName.substring(0,fileName.length()-9) + "\\newDocument.txt"));
    		semantic.convert(); 
    		writer.close();
            reader.close();	
            scan.close();
            // 8. clean up (closing file...etc)
       }
    	}
    }
}